package com.qf.hsr.ex.resolver;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author q
 * @version V1.0
 * @date 2021/11/15
 **/
@ControllerAdvice
public class ControllerGlobalExceptionResolver {

    //优先于GlobalExceptionResolver

    //先匹配精准的，如果没有匹配到，则匹配大的

    /**
     * 处理器出现 ArithmeticException，交给下面这个方法处理
     * @param e
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    public ModelAndView method01(Exception e){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("errorArithmetic");
        mav.addObject("exMsg","ArithmeticException.class");
        return mav;
    }

    /**
     * 处理器出现 ArithmeticException，交给下面这个方法处理
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public ModelAndView method02(Exception e){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("error");
        mav.addObject("exMsg","RuntimeException.class");
        return mav;
    }

    @ExceptionHandler({NumberFormatException.class})
    @ResponseBody
    public String method3(Exception e){
        return "NumberFormatException.class : " + e.getMessage();
    }


}
